package luanluan.control.scrollBar 
{
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import luanluan.control.buttons.LButton;
	import luanluan.control.managers.StageManager;
	
	/**
	 * 滚动条
	 * @author luanluan
	 */
	public class LScrollBar 
	{
		private var _topBtn:LButton;		//	*向上按钮
		private var _bottomBtn:LButton;		//	*向下按钮
		private var _bar:LButton;			//	*滑块
		
		private var _maxAmount:Number;		//	*最大总数
		private var _amount:Number;			//	*当前总数
		
		private var _enable:Boolean = false;
		
		private var _flag:MovieClip;
		
		public static var GAP:Number = 5;	//	*按上下按钮滑块移动距离（像素）
		
		/**
		 * 滚动条
		 * @param	topBtn 		向上按钮
		 * @param	bottomBtn 	向下按钮
		 * @param	bar			滑块
		 * @param	maxAmount	最大总数
		 */
		public function LScrollBar(topBtn:MovieClip , bottomBtn:MovieClip , bar:MovieClip , maxAmount:Number) 
		{
			_topBtn 	= new LButton(topBtn);
			_bottomBtn 	= new LButton(bottomBtn);
			_bar 		= new LButton(bar.bar);
			
			_maxAmount	= maxAmount;
			if (bar["flag"])
				_flag = bar["flag"];
			
			init();
		}
		
		/**
		 * 初始化
		 */
		private function init():void 
		{
			if (_flag)
			{
				_flag.mouseEnabled = false;
				_flag.mouseChildren = false;
			}
			
			_topBtn.onMouseClick = buttonClickHandler;
			_bottomBtn.onMouseClick = buttonClickHandler;
			
			_bar.onMouseDown = barDownHandler;
			
			amount = 0;
		}
		
		private function barDownHandler(e:MouseEvent):void 
		{
			
		}
		
		private function buttonClickHandler(e:MouseEvent):void 
		{
			
		}
		
		private function stageMouseUpHandler(e:MouseEvent):void
		{
			e.currentTarget.removeEventListener(e.type, arguments.callee);
		}
		
		/**
		 * 是否启用
		 */
		public function get enable():Boolean 	{	return _enable;	}
		public function set enable(value:Boolean):void 
		{
			_topBtn.enable = _bottomBtn.enable = _bar.enable = _enable = value;
			if (!value)
				StageManager.instance.stage.removeEventListener(MouseEvent.MOUSE_UP , stageMouseUpHandler);
		}
		
		/**
		 * 当前总数量
		 */
		public function get amount():Number		{	return _amount	}
		public function set amount(value:Number):void 
		{
			_amount = value;
			updateBarSize();
		}
		
		private function updateBarSize():void 
		{
			if (_amount <= _maxAmount)
			{
				_bar.enable = false;
				_bar.mc.visible = false;
			}
			else
			{
				_bar.enable = true;
				_bar.mc.visible = true;
				_bar.mc.scaleY = (_amount - _maxAmount) / _amount;
			}
			
			if (_flag)
			{
				_flag.visible = _bar.mc.visible;
				_flag.y = _bar.mc.y + _bar.mc.height / 2 - _flag.height / 2;
			}
		}
	}

}